package org.jadestudio.dsdown.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jadestudio.dsdown.entity.ManageAdminLogin;
import org.springframework.stereotype.Component;

import com.ibatis.sqlmap.client.SqlMapClient;
@Component
public class ManageLoginDao {
	@Resource
	private SqlMapClient sqlMapClient;
	private ManageAdminLogin manageAdmin;
	
	/**
	 * 用户登录
	 * @param manageAdminLogin
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public int userLogin(ManageAdminLogin manageAdminLogin,HttpServletRequest request) throws Exception{
		List lt = sqlMapClient.queryForList("test.adminLogin",manageAdminLogin);
		if(lt.size()>0){
			manageAdmin = (ManageAdminLogin) lt.get(0);
			HttpSession session = request.getSession();
			session.setAttribute("manageAdmin", manageAdmin);
			session.setMaxInactiveInterval(3600*24);
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * 用户登出
	 * @param request
	 * @throws Exception
	 */
	public void userLoginOut(HttpServletRequest request) throws Exception{
		HttpSession session = request.getSession();
		session.removeAttribute("manageAdmin");
	}
	/**
	 * session自检
	 * @param request
	 * @throws Exception
	 */
	public int sessionRecheck(HttpServletRequest request) throws Exception{
		HttpSession session = request.getSession();
		manageAdmin = (ManageAdminLogin)session.getAttribute("manageAdmin");
		//int checkFlag= this.userLogin(manageAdmin, request);
		if(manageAdmin!=null){
			String userName = manageAdmin.getLogin_name()!=null?manageAdmin.getLogin_name().toString():"";
			String userPass = manageAdmin.getPass_word()!=null?manageAdmin.getPass_word().toString():"";
			if(!userName.equals("") && !userPass.equals("")){
				/************************************************
				 *
				 *  如果服务器压力大 或者不爽可以把下面的去掉,不过安全性也就不能保证了
				 *  （可能有伪造session的情况）
				 * 
				 ************************************************/
				//List lt = sqlMapClient.queryForList("test.adminLogin",manageAdmin);
				//if(lt.size()>0){
					return 1;//正常session
				//}else{
				//	return 0;//非法session
				//}
			}else{
				return 0;//用户名密码丢失
			}
		}else{
			return 0;//session为空
		}
	}
	
	
	
	
}
